package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.entity.Movies;
import com.itheima.result.Result;
import com.itheima.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电影管理
 */
@RestController
@RequestMapping("movies")
public class MovieController {

    @Autowired
    private MovieService movieService;

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return PageInfo<Movies>
     */
    @GetMapping("list")
    public Result findByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Integer cid,
            String keyword) {
        //调用service完成分页查询
        PageInfo<Movies> list = movieService.findByPage(pageNum, pageSize, cid, keyword);
        return Result.success(list);
    }


    /**
     * 查询所有
     *
     * @return List<Movies>
     */
    @GetMapping("all")
    public Result findAll() {
        //调用service完成分页查询
        List<Movies> list = movieService.findAll();
        return Result.success(list);
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return movies
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        Movies movies = movieService.findById(id);
        return Result.success(movies);
    }

    /**
     * 新增或修改
     *
     * @param movies 电影对象
     * @return 无
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Movies movies) {
        if (movies.getId() == null) {
            // 新增
            movieService.save(movies);
        } else {
            // 修改
            movieService.update(movies);
        }
        return Result.success();
    }

    /**
     * 根据id删除
     *
     * @param id 主键
     * @return 无
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id) {
        movieService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids 主键数组
     * @return 无
     */
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(Integer[] ids) {
        movieService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 设置电影为轮播图
     *
     * @param id 主键
     * @return 无
     */
    @PostMapping("/setMovieToBanner")
    public Result setMovieToBanner(Integer id) {
        movieService.setMovieToBanner(id);
        return Result.success();
    }
}